﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.wechat
{
    public partial class article : weikeweixin.web.admin.cs.adminbase
    {
        w_article_dal articledal = new w_article_dal();
        protected w_article_model articlemodel = new w_article_model();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"])) {
                articlemodel = articledal.GetModel(int.Parse(Request.QueryString["id"]));
            }
            switch (Request.Form["action"]) {
                case "save":
                    add();
                    break;
            }
        }

        //新增或编辑
        private void add(){
            if (!string.IsNullOrEmpty(Request.Form["id"]))
            {
                articlemodel = articledal.GetModel(int.Parse(Request.Form["id"]));
            }
            articlemodel.article_title = Request.Form["article_title"].ToString();
            articlemodel.article_description = Request.Form["article_description"].ToString();
            articlemodel.article_type = Request.Form["article_type"].ToString();
            if (Request.Form["article_type"].ToString() == "href")
            {
                articlemodel.article_url = Request.Form["article_url"].ToString();
            }
            else
            {
                articlemodel.article_content = Request.Form["article_content"].ToString();
            }
            articlemodel.article_picurl = Request.Form["article_picurl"].ToString();
            articlemodel.article_addtime = DateTime.Now;
            articlemodel.article_sort = int.Parse(Request.Form["article_sort"].ToString());
            if (!string.IsNullOrEmpty(Request.Form["article_id"]))
            {
                articlemodel.article_layid = int.Parse(Request.Form["article_id"]);
            }
            else
            {
                articlemodel.article_layid = 0;
            }
            articlemodel.useer_id = int.Parse(user_id);


            if (!string.IsNullOrEmpty(Request.Form["article_id"]))
            {
                if (!string.IsNullOrEmpty(Request.Form["id"]))
                {
                    bool re = articledal.Update(articlemodel);
                    if (re)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"articlelist.aspx?article_id=" + Request.Form["article_id"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"article.aspx?article_id=" + Request.Form["article_id"] + "\",\"error\":\"\"}");
                        Response.End();
                    }

                }
                else
                {
                    int re = articledal.Add(articlemodel);
                    if (re > 0)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"articlelist.aspx?article_id=" + Request.Form["article_id"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"article.aspx?article_id=" + Request.Form["article_id"] + "\",\"error\":\"\"}");
                        Response.End();
                    }
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Request.Form["id"]))
                {
                    bool re = articledal.Update(articlemodel);
                    if (re)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"articlelist.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"article.aspx\",\"error\":\"\"}");
                        Response.End();
                    }

                }
                else
                {
                    int re = articledal.Add(articlemodel);
                    if (re > 0)
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"articlelist.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"article.aspx\",\"error\":\"\"}");
                        Response.End();
                    }
                }
            }
        }
    }
}